/* $Header: diag.h,v 2.3 04/04/20 13:51:38 przybyls Exp $ */

/***********************************************************************/
/* INCLUDE FILE NAME:  diag.h                                          */
/* ==================                                                  */
/*                                                                     */
/* Author:  Lesley Freed                                               */
/*                                                                     */
/* COMMENT:                                                            */
/*                                                                     */
/***********************************************************************/

#ifndef DIAG_H
#define DIAG_H

/*****************************/
/*   IFDEFS                  */
/*****************************/


/*****************************/
/*   SYMBOLIC CONSTANTS      */
/*****************************/


/*****************************/
/*   MACROS                  */
/*****************************/

#define POWER_ON_TEST_IN_PROGRESS (Power_On_Test_Complete == FALSE)
#define POWER_ON_TEST_COMPLETE    (Power_On_Test_Complete == TRUE)

/*****************************/
/*   TYPEDEFS                */
/*****************************/

typedef enum
{
  FROM_NULL     = 0,
  FROM_IO       = 1,
  FROM_STARTUP  = 2,
  FROM_KEYBOARD = 3
} DIAG_FROM;

typedef struct
{
  BIT8     attn_out;
  INT32    dac_out;
  U_INT16  attn_min;
  U_INT16  attn_max;
  U_INT16  dac_min;
  U_INT16  dac_max;

}  TEST_SIG_DATA;

typedef enum
{
  BUSY,
  IDLE,
  DONE

} DIAG_STATUS;

typedef enum
{
  NO_TEST,
  LOOP_BACK_TEST,
  MUX_TIME_RESPONSE_TEST,
  HOST_RS232_TEST,
  HOST_RS232_DATA_LOOPBK_TEST,
  HOST_RS232_CNTL_LOOPBK_TEST,
  OBC_RS232_TEST,
  OBC_RS232_DATA_LOOPBK_TEST,
  OBC_RS232_CNTL_LOOPBK_TEST,
  INJ_1_RS232_TEST,
  INJ_1_RS232_DATA_LOOPBK_TEST,
  INJ_1_RS232_CNTL_LOOPBK_TEST,
  INJ_2_RS232_TEST,
  INJ_2_RS232_DATA_LOOPBK_TEST,
  INJ_2_RS232_CNTL_LOOPBK_TEST,
  TRAY_RS232_TEST,
  TRAY_RS232_DATA_LOOPBK_TEST,
  TRAY_RS232_CNTL_LOOPBK_TEST

} TEST_TYPE;

typedef enum
{
  MOD_BAD_CHECKSUM,
  MOD_BAD_MODULE,
  MOD_EEPROM_REV,
  MOD_UNSUPPORTED_TYPE,
  MOD_BAD_PID,
  MOD_BAD_CONSTANTS,
  AUX_3_BAD_CONSTANTS,
  AUX_4_BAD_CONSTANTS,
  AUX_5_BAD_CONSTANTS

} MODULE_FAULT_TYPE;

typedef struct
{
   BIT8         free;                  /* boolean var for availability of     */
                                       /* diagnostic resources                */
   TEST_TYPE    type;                  /* type of test activated              */
   DIAG_FROM    from;                  /* calling routine source              */
   DIAG_RESULT  result;                /* result of last diagnostic executed  */
   DIAG_STATUS  status;                /* status of last diagnostic executed  */
   U_INT8       counter;               /* generic counter                     */
   U_INT8       counter2;              /* generic counter                     */
   DIAG_RESULT  ( * diag_fcn )(void);  /* pointer to diagnostic to run        */
   U_INT32      timer;                 /* 512 Hz tick counter                 */
   U_INT16      index;                 /* generic index pointer               */
   INT32        value[20];             /* generic values                      */

} DIAG_STATE;

/* The following enum is used to define the options under the
   diagnostics option on the keyboard.                        */
typedef enum
{
 OPT_PID_DIAG,
 OPT_OVEN_PID_DIAG,
 OPT_DETA_PID_DIAG,
 OPT_DETB_PID_DIAG,
 OPT_INJA_PID_DIAG,
 OPT_INJB_PID_DIAG,
 OPT_AUXA_PID_DIAG,
 OPT_AUXB_PID_DIAG,
 OPT_OVEN_CRYO_PID_DIAG,
 OPT_INJA_CRYO_PID_DIAG,
 OPT_INJB_CRYO_PID_DIAG,
 OPT_AUXA_CRYO_PID_DIAG,
 OPT_AUXB_CRYO_PID_DIAG,

/* Detector diagnostics */
 OPT_F_DET_DIAG,
 OPT_F_DET_BD_DIAG,
 OPT_F_DET_PNEU_MOD,
 OPT_F_DET_FUEL_GAS,
 OPT_F_DET_FUEL_PRES_SENSOR,
 OPT_F_DET_FUEL_CONTROL,
 OPT_F_DET_UTIL_GAS,
 OPT_F_DET_UTIL_PRES_SENSOR,
 OPT_F_DET_UTIL_CONTROL,
 OPT_F_DET_MAKEUP_GAS,
 OPT_F_DET_MAKEUP_PRES_SENSOR,
 OPT_F_DET_MAKEUP_CONTROL,
 OPT_B_DET_DIAG,
 OPT_B_DET_BD_DIAG,
 OPT_B_DET_PNEU_MOD,
 OPT_B_DET_FUEL_GAS,
 OPT_B_DET_FUEL_PRES_SENSOR,
 OPT_B_DET_FUEL_CONTROL,
 OPT_B_DET_UTIL_GAS,
 OPT_B_DET_UTIL_PRES_SENSOR,
 OPT_B_DET_UTIL_CONTROL,
 OPT_B_DET_MAKEUP_GAS,
 OPT_B_DET_MAKEUP_PRES_SENSOR,
 OPT_B_DET_MAKEUP_CONTROL,

/* Inlet diagnostics */
 OPT_F_INLET_DIAG,
 OPT_F_INL_PNEU_MOD,
 OPT_F_INL_FLOW_DIAG,
 OPT_F_INL_P_SENSOR_DIAG,
 OPT_F_INL_F_SENSOR_DIAG,
 OPT_F_INL_CONTROL,
 OPT_F_INL_SS_VLV_CONTROL,
 OPT_F_INL_PRES_CONTROL,
 OPT_F_INL_FLOW_CONTROL,
 OPT_F_INL_PURGE_REG,
 OPT_B_INLET_DIAG,
 OPT_B_INL_PNEU_MOD,
 OPT_B_INL_FLOW_DIAG,
 OPT_B_INL_P_SENSOR_DIAG,
 OPT_B_INL_F_SENSOR_DIAG,
 OPT_B_INL_CONTROL,
 OPT_B_INL_SS_VLV_CONTROL,
 OPT_B_INL_PRES_CONTROL,
 OPT_B_INL_FLOW_CONTROL,
 OPT_B_INL_PURGE_REG,

/* Aux diagnostics */
 OPT_AUX_PRES_DIAG,
 OPT_AUX_PNEU_MOD,
 OPT_AUX_3_DIAG,
 OPT_AUX_3_PRES_SENSOR,
 OPT_AUX_3_CONTROL,
 OPT_AUX_4_DIAG,
 OPT_AUX_4_PRES_SENSOR,
 OPT_AUX_4_CONTROL,
 OPT_AUX_5_DIAG,
 OPT_AUX_5_PRES_SENSOR,
 OPT_AUX_5_CONTROL,

/* Hidden diagnostics */
 OPT_HIDDEN_F_DET_DIAG,
 OPT_HIDDEN_B_DET_DIAG,
 OPT_PCB_EDIT,
 OPT_EEPROM_EDIT,

/* Instrument status/config */
 OPT_INST_STAT_DIAG,
 OPT_LAN_STAT_DIAG,
 OPT_INST_DIAG_CONFIG,
 OPT_DISABLE_CLEAR_DIAG,
 OPT_HACKSAW_MODE,
 OPT_ACTUAL_EQUALS_SETPT,
 OPT_USE_THERMAL_SETPT,

/* Electronics diagnostics */
 OPT_ELEC_DIAG,
 OPT_COMMUNICATIONS,
 OPT_LOOP_BACK,
 OPT_MUX_DIAG,
 OPT_PNEU_MUX_DIAG,
 OPT_PNEU_BD_DIAG,
 OPT_SIG_DSP_DIAG,
 OPT_POWER_SHARING,

/* Signal diagnostics */
 OPT_SIGNAL_DIAG,
 OPT_PLOT_MEM,

/* Sampler diagnostics */
OPT_F_INJ_ALIGN,
OPT_B_INJ_ALIGN,

 OPT_EXIT_TEST,

 OPT_CLEAR_DIAG

} DIAG_OPTION;

/* The following enum is used to define the test options under
   the diagnostics option on the keyboard.                     */
typedef enum
{
 OPT_HOST_RS232,
 OPT_HOST_RS232_DATA_LOOPBK,
 OPT_HOST_RS232_CNTL_LOOPBK,
 OPT_OBC_RS232,
 OPT_OBC_RS232_DATA_LOOPBK,
 OPT_OBC_RS232_CNTL_LOOPBK,
 OPT_INJ_1_RS232,
 OPT_INJ_1_RS232_DATA_LOOPBK,
 OPT_INJ_1_RS232_CNTL_LOOPBK,
 OPT_INJ_2_RS232,
 OPT_INJ_2_RS232_DATA_LOOPBK,
 OPT_INJ_2_RS232_CNTL_LOOPBK,
 OPT_TRAY_RS232,
 OPT_TRAY_RS232_DATA_LOOPBK,
 OPT_TRAY_RS232_CNTL_LOOPBK

} TEST_OPTION;

/* The following enum is used to define the pneu diagnostics on/off
   setpts under the diagnostics option on the keyboard.        */
typedef enum
{
   F_SS_MAN_VLV,
   F_SS_VLV_CTL,
   B_SS_MAN_VLV,
   B_SS_VLV_CTL,
   F_INL_F_CONST_DC,
   B_INL_F_CONST_DC,
   F_INL_P_CONST_DC,
   B_INL_P_CONST_DC,
   F_DET_FUEL_GAS_DC,
   F_DET_UTIL_GAS_DC,
   F_DET_MKUP_GAS_DC,
   B_DET_FUEL_GAS_DC,
   B_DET_UTIL_GAS_DC,
   B_DET_MKUP_GAS_DC,
   AUX_3_CONST_DC,
   AUX_4_CONST_DC,
   AUX_5_CONST_DC,
   ZERO_ALL_SENSORS_INDEX,
   PURGE_REG_PROG_NOW

} PNEU_DIAG_ON_OFF;

/*****************************/
/*   EXTERNAL  VARIABLES     */
/*****************************/

extern BIT8                Actual_Equals_Setpt;
extern BIT8                Use_Thermal_Setpt;

extern BIT8                Mux_Adc_Time_Response_Test;

extern DIAG_STATE          Diag_State;
extern BIT8                Diag_Mode;
extern const TEST_SIG_DATA TEST_SIG_TABLE[];
extern INT32               DT_Parm[3];

#define                    BUF_STAT_LIM 44
extern BIT8                Buffer_Status[BUF_STAT_LIM];

/*****************************/
/*   FUNCTION PROTOTYPES     */
/*****************************/

        /* diag.c functions */

        BIT8   GetDiagResource( TEST_TYPE type, DIAG_FROM from );
        void   FreeDiagResource( void );
        void   FreeIODiagResource( void );
        void   HandleDiagnostic( void );
        void   ExitTest( BIT32 from );
        UI_ERR SetDiagMode( ON_OFF on_off );
        void   ClearHacksawSetpts( void );
        BIT8   GetDemoMode( void );

        /* diag_ui.c functions */

        DIAG_STATUS GetDiagStatus( void );
        DIAG_RESULT GetDiagResult( void );
        TEST_TYPE   GetDiagTestType( void );

        void        SetDiagStatus( DIAG_STATUS status );
        void        SetDiagResult( DIAG_RESULT result );

        INT16       GetSerialNumber( BIT8 *str );
        UI_ERR      SetSerialNumber( BIT8 *serial_no );
        TIME        GetDaysHoursMinutesSeconds( U_INT32 seconds );
        INT16       TransferVersion( BIT8 *line );
        UI_ERR      SetUptime( INT32 time );
        UI_ERR      SetRunCount( INT32 count );

        INT32       GetWhenManufacturedDate( void );

        INT32       Get5VMonitorInmV( void );
        INT32       Get15VposMonitorInmV( void );
        INT32       Get15VnegMonitorInmV( void );
        INT32       Get24VMonitorInmV( void );
        INT32       GetZoneVoltageIn10mV( void );

        /* det_diag.c functions */

        UI_ERR      SetDiagDetInput(POSITION position, BIT32 bit,ON_OFF on_off);
        UI_ERR      SetDiagDetLinearMode(POSITION position, ON_OFF on_off);

        /* mem_diag.c functions */

        void        PokeMemory( U_INT32 *pmem, DATA_SIZE size, INT32 value );
        INT32       PeekMemory( U_INT32 *pmem, DATA_SIZE size );

        /* sigpath/sig_diag.c functions */

        INT32        GetSignalDspStatus( void );

        /* pneu_diag.c functions */
        INT32 GetAuxPresDiagSetpt( BIT8 aux_number );
        INT32 GetAuxPresDiagActual( BIT8 aux_number );
        INT32 GetInletPresDiagSetpt( BIT8 position );
        INT32 GetInletPresDiagActual( BIT8 position );
        INT32 GetInletFlowDiagSetpt( BIT8 position );
        INT32 GetInletFlowDiagActual( BIT8 position );
        INT32 GetDetPresDiagSetpt( BIT8 position, U_INT8 gas_num );
        INT32 GetDetPresDiagActual( BIT8 position, U_INT8 gas_num );
        INT32 GetDetFlowDiagSetpt( BIT8 position, U_INT8 gas_num );
        INT32 GetDetFlowDiagActual( BIT8 position, U_INT8 gas_num );
        INT32 GetPresDiagValueFromSignal( INT32 signal );

#endif
